package cz.cvut.fel.nur.rcp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import android.app.Application;
import cz.cvut.fel.nur.rcp.classes.Alarm;
import cz.cvut.fel.nur.rcp.classes.Alarm.AlarmType;
import cz.cvut.fel.nur.rcp.classes.HistoryItem;
import cz.cvut.fel.nur.rcp.classes.Ingredient;
import cz.cvut.fel.nur.rcp.classes.Ingredient.QuantityType;
import cz.cvut.fel.nur.rcp.classes.IngredientWrapper;
import cz.cvut.fel.nur.rcp.classes.ListItem;
import cz.cvut.fel.nur.rcp.classes.Recipe;

public class GlobalClass extends Application {

	// Global variable to hold a String
	private int resource1;
	private int resource2;
	private int stahovani;
	private HashMap<Integer, Recipe> recipes;
	private HashMap<Integer, Integer> pantry;
	private HashMap<Integer, Ingredient> allIngredients;
	private HashMap<Integer, Alarm> alarms;
	private List<HistoryItem> history;
	private HashMap<Ingredient, ListItem> list;
	private int film;
	private String step = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi iaculis, augue non aliquam bibendum, mauris turpis varius nunc, non luctus mi ligula in magna. Etiam malesuada auctor tellus, vel consequat sapien dignissim at. Duis ultricies placerat varius. Donec malesuada quis erat eu congue. Etiam vehicula ultricies venenatis. Aliquam tempor arcu enim, eu auctor odio luctus a. Curabitur sit amet ipsum et nibh pellentesque accumsan vel ac nulla. In lacinia libero eu vehicula pharetra.";

	private IngredientWrapper modifiedIngredient;
	
	public ArrayList<IngredientWrapper> addedIngredientList;
	
	public Recipe currentRecipe;
	public boolean usePantry;
	public int portions;
	public IngredientWrapper currentIngredient, lastIngredient;
	public int currentAmount;
	public Alarm currentAlarm;

	@Override
	public void onCreate() {
		addedIngredientList = new ArrayList<IngredientWrapper>();
		
		// Initialize resources or load from preferences
		resource1 = 0;// film1 viditelnost v ulozeneFilmy
		resource2 = 0;// serial1 viditelnost v ulozeneFilmy
		stahovani = 0;
		film = 0;// film 1 == 0, serial 1 == 1
		recipes = new HashMap<Integer, Recipe>();
		HashMap<Ingredient, Integer> ingredients = new HashMap<Ingredient, Integer>();
		List<String> steps = new ArrayList<String>();
		steps.add(step);
		steps.add(step);
		steps.add(step);
		pantry = new HashMap<Integer, Integer>();
		allIngredients = new HashMap<Integer, Ingredient>();
		
		Ingredient i1 = new Ingredient("Ingredience 1", QuantityType.GRAM);
		allIngredients.put(i1.hashCode(), i1);
		Ingredient i2 = new Ingredient("Ingredience 2", QuantityType.MILILITER);
		allIngredients.put(i2.hashCode(), i2);
		Ingredient i3 = new Ingredient("Ingredience 3", QuantityType.GRAM);
		allIngredients.put(i3.hashCode(), i3);
		Ingredient i4 = new Ingredient("Ingredience 4", QuantityType.ITEM);
		allIngredients.put(i4.hashCode(), i4);
		
		pantry.put(i1.hashCode(), 500);
		pantry.put(i2.hashCode(), 1620);
		pantry.put(i3.hashCode(), 500);
		pantry.put(i4.hashCode(), 20);
		
		ingredients.put(i1, 50);
		ingredients.put(i2, 820);
		ingredients.put(i3, 40);
		ingredients.put(i4, 2);
		
		recipes.put("Recept 1".hashCode(), new Recipe("Recept 1", 30, 1000, 100, 100, 100, ingredients, "krabi", "video", steps));
		recipes.put("Recept 2".hashCode(), new Recipe("Recept 2", 30, 1000, 100, 100, 100, ingredients, "krabi", "video", steps));
		recipes.put("Recept 3".hashCode(), new Recipe("Recept 3", 30, 1000, 100, 100, 100, ingredients, "krabi", "video", steps));

		alarms = new HashMap<Integer, Alarm>();
		alarms.put("Alarm 1".hashCode(), new Alarm(false, "Alarm 1", AlarmType.Snidane, new boolean[] { true, false, false, true, true, false, false }, new Date(1970, 1, 1, 12, 0, 0)));
		alarms.put("Alarm 2".hashCode(), new Alarm(true, "Alarm 2", AlarmType.Snidane, new boolean[] { true, false, false, true, true, false, false }, new Date(1970, 1, 1, 12, 0, 0)));
		alarms.put("Alarm 3".hashCode(), new Alarm(false, "Alarm 3", AlarmType.Snidane, new boolean[] { true, false, false, true, true, false, false }, new Date(1970, 1, 1, 12, 0, 0)));

		history = new ArrayList<HistoryItem>();
		history.add(new HistoryItem(recipes.get("Recept 1".hashCode()), new Date()));
		history.add(new HistoryItem(recipes.get("Recept 1".hashCode()), new Date()));
		history.add(new HistoryItem(recipes.get("Recept 1".hashCode()), new Date()));
		history.add(new HistoryItem(recipes.get("Recept 2".hashCode()), new Date(113, 11, 13, 22, 0, 0)));
		history.add(new HistoryItem(recipes.get("Recept 2".hashCode()), new Date(113, 11, 13, 21, 0, 0)));
		history.add(new HistoryItem(recipes.get("Recept 3".hashCode()), new Date(113, 11, 12, 22, 0, 0)));
		history.add(new HistoryItem(recipes.get("Recept 3".hashCode()), new Date(113, 11, 12, 21, 0, 0)));

		list = new HashMap<Ingredient, ListItem>();
		list.put(i1, new ListItem(i1, 10));
		list.put(i2, new ListItem(i2, 10));
		list.put(i3, new ListItem(i3, 10));
		list.put(i4, new ListItem(i4, 10));
	}

	public void addToList(Ingredient i, int a) {
		if (list.containsKey(i)) {
			list.get(i).addAmount(a);
			return;
		}
		list.put(i, new ListItem(i, a));
	}

	
	public HashMap<Ingredient, ListItem> getList() {
		return list;
	}

	public void removeFromList(Ingredient i) {
		list.remove(i);
	}

	public Recipe getRecipe(int hashcode) {
		return recipes.get(hashcode);
	}

	public Alarm getAlarm(int hashcode) {
		return alarms.get(hashcode);
	}

	public List<Alarm> getAlarms() {
		return new ArrayList<Alarm>(alarms.values());
	}
	
	public void removeAlarm(Alarm a) {
		alarms.remove(a.getName().hashCode());
	}
	
	public void addAlarm(Alarm a) {
		alarms.put(a.getName().hashCode(), a);
	}

	public List<Recipe> getRecipes() {
		return new ArrayList<Recipe>(recipes.values());
	}
	
	public void removeRecipe(int hash) {
		recipes.remove(hash);
	}

	public void removeRecipe(Recipe r) {
		removeRecipe(r.hashCode());
	}
	
	public int getResource1() {
		return resource1;
	}

	public void setResource1(int s) {
		resource1 = s;
	}

	public int getResource2() {
		return resource2;
	}

	public void setResource2(int s) {
		resource2 = s;
	}

	public int getResource3() {
		return stahovani;
	}

	public void setResource3(int s) {
		stahovani = s;
	}

	public int getFilm() {
		return film;
	}

	public void setFilm(int s) {
		film = s;
	}

	
	public ArrayList<IngredientWrapper> getIngredientsInPantry() {
		ArrayList<IngredientWrapper> list = new ArrayList<IngredientWrapper>();
		
		for(Entry<Integer, Integer> entry : pantry.entrySet()) {
			IngredientWrapper i = new IngredientWrapper(allIngredients.get(entry.getKey()), entry.getValue());
			list.add(i);
		}
		
		return list;
	}
	
	public Ingredient findIngredientInAll(String name) {
		
		return allIngredients.get(name.hashCode());
	}
	
	public void createNewIngredient(Ingredient ing) {
		allIngredients.put(ing.hashCode(), ing);
	}
	
	public void putIngredientInPantry(Ingredient ing, int quantity) {
		
		Integer pantryIngQuant = pantry.get(ing.hashCode());
		if(pantryIngQuant == null) {
			allIngredients.put(ing.hashCode(), ing);
			pantry.put(ing.hashCode(), quantity);
		} else {
			pantry.put(ing.hashCode(), pantryIngQuant + quantity);
		}
	}
	
	public void setIngredientInPantryQuantity(Ingredient ing, int quantity) {
		
		Integer pantryIngQuant = pantry.get(ing.hashCode());
		if(pantryIngQuant == null) {

		} else {
			pantry.put(ing.hashCode(), quantity);
		}
	}

	public IngredientWrapper getModifiedIngredient() {
		return modifiedIngredient;
	}


	public void setModifiedIngredient(IngredientWrapper modifiedIngredient) {
		this.modifiedIngredient = modifiedIngredient;
	}

	
	public void addToHistory(Recipe r, int p) {
		history.add(new HistoryItem(r, new Date(), p));
	}

	public int getMissingIngredient(Ingredient i, Integer integer) {
		int amount = pantry.get(i.hashCode()) - integer;
		return -amount;
	}

	public List<HistoryItem> getHistory() {
		return history;
	}
}
